{# COMPOUNDS #}
<h4 title='{{ 'Toggle visibility'|trans }}' data-action='toggle-next' data-opened-icon='fa-caret-down' data-closed-icon='fa-caret-right' class='ml-3 d-inline togglable-section-title' tabindex='0' role='button' aria-expanded='true'><i class='fas fa-caret-down fa-fw mr-2'></i>{{ 'Compounds'|trans }} (<span id='compoundsCount'>{{ Entity.entityData.compounds|length }}</span>)</h4>
<div data-save-hidden='compoundDiv' data-count-for='compoundsCount' class='ml-4'>
  <div class='mb-2' id='compoundDiv'>
    {% if Entity.entityData.compounds %}
      {% macro fpList(key, label, value) %}
        <li class='list-group-item'><label for='{{ key }}'>{{ label }}</label>
          <div class='input-group'>
            <div class='input-group-prepend'>
              <button title='{{ 'Copy to clipboard'|trans }}' class='btn btn-outline-secondary' type='button' data-action='copy-to-clipboard' data-target='{{ key }}'><i class='fas fa-clone'></i></button>
            </div>
            <input readonly class='form-control' id='{{ key }}' value='{{ value }}' />
          </div>
        </li>
      {% endmacro %}
      {% macro compoundPicto(compound) %}
         {% set hazards = {
          'is_corrosive': {'code': '05', 'alt': 'Corrosive'},
          'is_serious_health_hazard': {'code': '08', 'alt': 'Serious Health Hazard'},
          'is_explosive': {'code': '01', 'alt': 'Explosive'},
          'is_flammable': {'code': '02', 'alt': 'Flammable'},
          'is_gas_under_pressure': {'code': '04', 'alt': 'Gas under pressure'},
          'is_hazardous2env': {'code': '09', 'alt': 'Hazardous to environment'},
          'is_hazardous2health': {'code': '07', 'alt': 'Hazardous to health'},
          'is_oxidising': {'code': '03', 'alt': 'Oxidising'},
          'is_toxic': {'code': '06', 'alt': 'Toxic'}
        } %}
        {% for property, data in hazards %}
          {% if attribute(compound, property) %}
            <img src='assets/images/ghs/GHS{{ data.code }}.svg' alt='{{ data.alt }}' title='{{ data.alt }}' />
          {% endif %}
        {% endfor %}

        {% set hazards2 = {
          'is_radioactive': {'icon': 'radiation', 'alt': 'Radioactive'},
          'is_antibiotic': {'icon': 'bacterium', 'alt': 'Antibiotic'},
          'is_antibiotic_precursor': {'icon': 'bacterium', 'alt': 'Antibiotic precursor'},
          'is_drug': {'icon': 'pills', 'alt': 'Drug'},
          'is_drug_precursor': {'icon': 'pills', 'alt': 'Drug precursor'},
          'is_explosive_precursor': {'icon': 'explosion', 'alt': 'Explosive precursor'},
          'is_cmr': {'icon': 'skull-crossbones', 'alt': 'Carcinogenic, mutagenic and reprotoxic'},
          'is_controlled': {'icon': 'shield', 'alt': 'Controlled substance'},
          'is_nano': {'icon': 'atom', 'alt': 'Nanomaterial'},
          'is_ed2health': {'icon': 'atom', 'alt': 'Endocrine disruptor for human health'},
          'is_ed2env': {'icon': 'atom', 'alt': 'Endocrine disruptor for the environment'},
          'is_pbt': {'icon': 'atom', 'alt': 'PBT'},
          'is_pmt': {'icon': 'atom', 'alt': 'PMT'},
          'is_vpvb': {'icon': 'atom', 'alt': 'vPvB'},
          'is_vpvm': {'icon': 'atom', 'alt': 'vPvM'}
        } %}
        {% for property, data in hazards2 %}
          {% if attribute(compound, property) %}
            {# wrapped in a <span> to allow the tooltip to appear on hover #}
            <span title='{{ data.alt|trans }}'>
              <i class='fas fa-2x fa-{{ data.icon }} mr-2'></i>
            </span>
          {% endif %}
        {% endfor %}
      {% endmacro %}
      {% for fp in Entity.entityData.compounds %}
        <div class='mt-3 p-2 countable hl-hover-superlight rounded d-flex justify-content-between'>
        <details>
          <summary>
            <h5 class='d-inline togglable-section-title'>{{ fp.name|default('Name unset'|trans) }} {{ _self.compoundPicto(fp) }}</h5>
          </summary>
          <ul class='mt-2 list-group'>
          {{ _self.fpList('cas_' ~ fp.id, 'CAS', fp.cas_number) }}
          {{ _self.fpList('iupac_name_' ~ fp.id, 'IUPAC Name', fp.iupac_name) }}
          {{ _self.fpList('molecular_weight_' ~ fp.id, 'Molecular weight'|trans, fp.molecular_weight) }}
          {{ _self.fpList('molecular_formula_' ~ fp.id, 'Molecular formula'|trans, fp.molecular_formula) }}
          {{ _self.fpList('inchi_' ~ fp.id, 'InChI', fp.inchi) }}
          {{ _self.fpList('inchi_key_' ~ fp.id, 'InChIKey', fp.inchi_key) }}
          {{ _self.fpList('smiles_' ~ fp.id, 'SMILES', fp.smiles) }}
          <li class='list-group-item'>
            <div class='d-flex justify-content-between'>
              {% if fp.smiles %}
                <a target='_blank' class='btn btn-ghost mr-1' rel='noopener' href='/compounds.php?search_fp_smi={{ fp.smiles|url_encode }}'>
                  {{ 'Search similar compounds'|trans }}
                </a>
              {% endif %}
              <a target='_blank' class='btn btn-ghost mr-1' rel='noopener' href='/api/v2/compounds/{{ fp.id }}'>{{ 'View in JSON'|trans }}</a>
              {% if fp.pubchem_cid %}
                <a target='_blank' class='btn btn-ghost mr-1 external-link' rel='noopener' href='https://pubchem.ncbi.nlm.nih.gov/compound/{{ fp.pubchem_cid }}'>
                  {{ 'View on PubChem'|trans }}
                </a>
              {% endif %}
            </div>
          </li>
          </ul>
        </details>
        {# DELETE #}
        {% if not Entity.isReadOnly %}
          <button type='button' data-action='delete-compound-link'  aria-label='{{ 'Remove compound'|trans }}' data-id='{{ fp.id }}' class='btn p-2 hl-hover-gray lh-normal border-0 my-n2'><i class='fas fa-fw fa-lg fa-trash-alt'></i></button>
        {% endif %}
        </div>
      {% endfor %}
    {% endif %}
  </div>
  {# ADD COMPOUND LINK #}
  {% if mode == 'edit' %}
  <div class='input-group'>
    <div class='input-group-prepend'>
      <span class='input-group-text'><i class='fas fa-magnifying-glass'></i></span>
    </div>
    <input aria-label='{{ 'Link with compound'|trans }}' type='text' data-endpoint='compounds' data-autocomplete='compounds' id='addCompoundInput' class='form-control linkinput' data-id='{{ Entity.id }}' placeholder='{{ 'Search in local compounds database'|trans }}' />
    <div class='input-group-append'>
      <button class='btn btn-primary' aria-label='{{ 'Add link to a compound'|trans }}' type='button'>{{ 'Add'|trans }}</button>
    </div>
  </div>
  {% endif %}
</div>
